<template>
  <div class="details">
    <el-card>
      <el-tabs>
        <el-tab-pane label="员工账户信息">
          <el-form ref="form" label-width="80px" :model="form" :rules="rules">
            <el-form-item prop="username" label="账号">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item prop="password" label="密码">
              <el-input v-model="form.password" />
            </el-form-item>
            <el-form-item>
              <template>
                <el-button type="primary" @click="submit">更新</el-button>
                <el-button>取消</el-button>
              </template>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人信息">
          <component :is="'UserInfo'" :user-info="initUser" @update="update" />
        </el-tab-pane>
        <el-tab-pane label="员工账户信息" />
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser, sysUserPut } from '@/api/user.js'
export default {
  name: '',
  components: { UserInfo: () => import('./components/userInfo.vue') },
  data() {
    return {
      id: this.$route.params.id,
      user: '',
      initUser: {},
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        password: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取用户基本信息
    async getData() {
      const res = await sysUser(this.id)
      console.log('ge', res)
      res.data.formOfEmployment = res.data.formOfEmployment || 1
      this.form.username = res.data.username

      this.initUser = JSON.parse(JSON.stringify(res.data))
      this.user = res.data
    },
    // 更新
    async submit() {
      await sysUserPut({ ...this.user, ...this.form })
      this.$message.success('修改成功')
      this.initUser = { ...this.user, ...this.form }
      this.$router.push('/employees')
    },
    update() {
      this.user = JSON.parse(JSON.stringify(this.initUser))
      this.form.username = this.user.username
    }
  }
}
</script>
<style scoped lang="scss"></style>
